﻿#coding:utf-8
'''
Indexer
version 1.0
author:zheng lingjie
'''

import os
import time
import re

import PostingIndex


class Indexer:
        
    def __init__(self, dir):
        self.__docs_dir = dir
        self.__indexer = PostingIndex.PostingIndexer(self.__docs_dir)
    
    def __del__(self):
        '''析构函数'''        
    
    def buildIndex(self):
        PostingIndex.work(self.__indexer)

    def query_word(self, word):
        url_list = PostingIndex.query_word(self.__indexer, word)
        #url_list的每个成员是一个tuple，(url，单词出现次数)
        return url_list
    
    def query_sentence(self, sentence):
        url_list= PostingIndex.query_sentence(self.__indexer, sentence)
        #url_list的每个成员是一个string
        return url_list

    


if __name__ == '__main__':
    index = Indexer('crawled_at_2014-08-11_09-48')
    index.buildIndex()
    
    url_list = index.query_word("动漫")
    for url in url_list:
        print url[0],url[1]     #url,单词出现次数
    
    print '\n-----------------------------------\n'
        
    url_list1 = index.query_sentence("极影字幕社")
    for url in url_list1:
        print url
    